package cn.zxh.improvement.common.core.pipeline.def;

import cn.zxh.improvement.common.core.pipeline.AbstractEventFilter;
import cn.zxh.improvement.common.core.pipeline.EventFilter;
import cn.zxh.improvement.common.core.pipeline.FilterChainPipeline;
import cn.zxh.improvement.common.core.pipeline.PipelineFilterEnum;
import cn.zxh.improvement.common.core.pipeline.context.AbstractEventContext;
import cn.zxh.improvement.common.core.pipeline.selector.*;
import lombok.Getter;
import lombok.Setter;

import java.util.*;

public class DefTest {

    enum DefPipelineFilterEnum implements PipelineFilterEnum<DefPipelineFilterEnum> {
        /**
         * 默认
         */
        DEFAULT_PIPELINE_FILTER_ENUM("0000000", "默渠道"),
        ;
        private final String code;
        private final String name;

        DefPipelineFilterEnum(String code, String name) {
            this.code = code;
            this.name = name;
        }

        @Override
        public String getCode() {
            return this.code;
        }

        @Override
        public String getName() {
            return this.name;
        }
    }

    static class DefContext extends AbstractEventContext {

        @Getter
        @Setter
        private String context1;
        @Getter
        @Setter
        private String context2;

        @Getter
        @Setter
        private DefFilterSelectorRequest defFilterSelectorRequest;

        public DefContext(PipelineFilterEnum<DefPipelineFilterEnum> pipelineFilterEnum, FilterSelector selector) {
            super(pipelineFilterEnum, selector);
        }

        @Override
        public boolean continueChain() {
            return true;
        }
    }

    static class DefFilter extends AbstractEventFilter<DefContext> {
        @Override
        protected void handle(DefContext context) {
            System.out.println("context1");
            context.setContext1("context1");
        }
    }

    static class DefFilter2 extends AbstractEventFilter<DefContext> {
        @Override
        protected void handle(DefContext context) {
            System.out.println("context2");
            context.setContext2("context2");
        }
    }

    static class DefFilterSelectorRequest implements FilterSelectorRequest {

        @Setter
        private String biz;

        @Getter
        @Setter
        private String code;

        @Override
        public String biz() {
            return this.biz;
        }
    }

    public static FilterChainPipeline<DefContext, EventFilter<DefContext>> defPipeline() {
        // 这里的管理的，所有相关任务的管道
        FilterChainPipeline<DefContext, EventFilter<DefContext>> filterChainPipeline = new FilterChainPipeline<>();
        filterChainPipeline.addFirst("用户逻辑1", new DefFilter());
        filterChainPipeline.addFirst("用户逻辑2", new DefFilter2());
        return filterChainPipeline;
    }

    public static void main(String[] args) {
        // 初始化配置 ------------开始--------------------
        Map<String, List<String>> bizChain = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add("DefFilter2");
        list.add("DefFilter");
        // default 业务名称
        bizChain.put("default", list);
        SelectorPropertiesConfig.init(bizChain);
        // 初始化配置 ------------结束-------------------

        DefaultFilterSelectorFactory<DefFilterSelectorRequest> defaultFilterSelectorRequestDefaultFilterSelectorFactory = new DefaultFilterSelectorFactory<>();
        DefFilterSelectorRequest request = new DefFilterSelectorRequest();
        // 业务名称
        request.setBiz("default");
        request.setCode("code");
        FilterSelector filterSelector = defaultFilterSelectorRequestDefaultFilterSelectorFactory.getFilterSelector(request);
        DefContext defContext = new DefContext(DefPipelineFilterEnum.DEFAULT_PIPELINE_FILTER_ENUM, filterSelector);
        defContext.setDefFilterSelectorRequest(request);
        // 按配置顺序执行，先执行2，再执行1
        defPipeline().getFilterChain().handle(defContext);
        System.out.println(defContext.getContext1() + ":" + defContext.getContext2());
    }


}
